package ast;

public class InputStatement extends Statement {
	// DataSignature类
	public AstItem LVal;
	// input框内的提示语
	public String promptString;
	
	public void preprocess(AstItem father) {
		LVal.preprocess(this);
	}
	
	public String getFormatString(AstItem x) {
		assert x instanceof PrimaryDataSignature;
		String type = ((PrimaryDataSignature) x).dataType;
		if (type.equals("int")) return "%d";
		else if (type.equals("long")) return "%ld";
		else if (type.equals("longlong")) return "%lld";
		else if (type.equals("double")) return "%lf";
		else if (type.equals("char")) return "\\n%c";
		else {
			assert type.equals("string");
			return "%s";
		}
	}
	
	public String toString() {
		String result = "";
		if(promptString != null) result = "printf(" + promptString + ");\n";
		if (((PrimaryDataSignature) LVal).dataType.equals("string")) { 
			result += "scanf(\"" + getFormatString(LVal) + "\"," + LVal.toString() + ");";
		} else {
			result += "scanf(\"" + getFormatString(LVal) + "\",&" + LVal.toString() + ");";
		}
		return result;
	}
	
	@Override
	public String toJsonString() {
		String json = "{";
		json += "\"Type\": \"Statement-" + type.toString() + "\",";
		json += "\"LVal\": " + LVal.toJsonString() + ",";
		json += "\"PromptString\": " + (promptString != null ? promptString : "\"\"");
		json += "}";
		return json;
	}
}
